import com.sun.xml.internal.bind.v2.TODO;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * Creat with IntelliJ IDEA
 * Description
 * User: mamba24
 * Date: 2022-06-30
 * Time: 19:44
 * To BE A Better Person-2020-3-2
 */
public class BinaryTree {

    static class TreeNode {
        public char val;
        public TreeNode left;//左孩子的引用
        public TreeNode right;//右孩子的引用

        public TreeNode(char val) {
            this.val = val;
        }
    }


    /**
     * 创建一棵二叉树 返回这棵树的根节点
     *
     * @return
     */
    public TreeNode createTree() {
        TreeNode A=new  TreeNode('A');
        TreeNode B=new  TreeNode('B');
        TreeNode C=new  TreeNode('C');
        TreeNode D=new  TreeNode('D');
        TreeNode E=new  TreeNode('E');
        TreeNode F=new  TreeNode('F');
        TreeNode G=new  TreeNode('G');
        A.left=B;
        A.right=C;
        B.left=D;
        B.right=E;
        C.left=F;
        C.right=G;
        return A;
    }

    // 前序遍历
    public void preOrder(TreeNode root) {
        if(root == null) return ;
        System.out.print(root.val+",");
        preOrder(root.left);
        preOrder(root.right);
    }

    // 中序遍历
    void inOrder(TreeNode root) {
        if(root == null) return ;
        preOrder(root.left);
        System.out.print(root.val+",");
        preOrder(root.right);
    }

    // 后序遍历
    void postOrder(TreeNode root) {
        if(root == null) return ;
        System.out.print(root.val+",");
        preOrder(root.left);
        preOrder(root.right);
    }

    public static int nodeSize;

    /**
     * 获取树中节点的个数：遍历思路
     */
    public void size(TreeNode root) {
        if(root == null) {
            return;
        }
        BinaryTree.nodeSize++;
        size(root.left);
        size(root.right);
    }

    /**
     * 获取节点的个数：子问题的思路
     *
     * @param root
     * @return
     */
    int size2(TreeNode root) {
        if(root == null) return 0;
        return size2(root.right)+size2(root.left)+1;
    }


    /*
     获取叶子节点的个数：遍历思路
     */
    public static int leafSize = 0;

    void getLeafNodeCount1(TreeNode root) {
        if(root == null ) return;
        if(root.left==null && root.right==null) BinaryTree.leafSize++;
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
    }

    /*
     获取叶子节点的个数：子问题
     */
    int getLeafNodeCount2(TreeNode root) {
        if(root.left!=null&&root.right!=null)
          return getLeafNodeCount2(root.left)+getLeafNodeCount2(root.right);
        return 1;
    }

    /*
    获取第K层节点的个数
     */
    // TODO: 2022/7/1 这里的代码还没测试，不确定能不能跑 
    public static int klevelsize=0;
    public static int level=0;
    int getKLevelNodeCount(TreeNode root, int k) {
        if(root.left==null ||root.right==null) return 0;
        if(BinaryTree.level==k) {
            BinaryTree.klevelsize++;
            return 0;
        }
        BinaryTree.level++;


        getKLevelNodeCount(root.left,k);
        getKLevelNodeCount(root.right,k);
        
        return klevelsize;
    }

    /*
     获取二叉树的高度
     时间复杂度：O(N)
     */
    int getHeight(TreeNode root) {
        if(root == null) return 0;
        int left=getHeight(root.left);
        int right=getHeight(root.right);
        return left>right?left+1:right+1;
    }


    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, char val) {
        if(root == null) return null;
        if(root.val==val) return root;
        TreeNode findNoderight=find(root.right,val);
        TreeNode findeNodeleft=find(root.left,val);
        if(findeNodeleft==null){
            return findNoderight;
        }else {
            return findeNodeleft;
        }
    }

    //层序遍历
    void levelOrder(TreeNode root) {
        if(root == null) return;
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode tmp=queue.peek();

            if(tmp.left != null){
                queue.offer(tmp.left);
            }
            if(tmp.right != null){
                queue.offer(tmp.right);
            }
            System.out.println(queue.poll().val);
        }
    }


    // 判断一棵树是不是完全二叉树
    boolean isCompleteTree(TreeNode root) {
        Queue<TreeNode> q=new LinkedList<>();
        q.offer(root);
        while(!q.isEmpty()){
            TreeNode tmp=q.poll();
            if(tmp != null){
                q.offer(tmp.left);
                q.offer(tmp.right);
            }else{
                break;
            }
        }

        //检测队列中是否还有元素
        while(!q.isEmpty()){
            TreeNode cur = q.peek();
            if (cur == null) {
                q.poll();
            } else {
                return false;
            }
        }
        return true;

    }


    /**
     * 非递归实现前序遍历
     * @param root
     */

    void preorderNor(TreeNode root){
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        //hh
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            System.out.print(node.val + " ");
            if (node.right != null) {
                stack.push(node.right);
            }
            if (node.left != null) {
                stack.push(node.left);
            }
        }


    }
    public void inorderNor(TreeNode root){
        Stack<TreeNode> stack = new Stack<TreeNode>();
        while(stack.size()>0 || root!=null) {
            //不断往左子树方向走，每走一次就将当前节点保存到栈中
            //这是模拟递归的调用
            if(root!=null) {
                stack.add(root);
                root = root.left;
                //当前节点为空，说明左边走到头了，从栈中弹出节点并保存
                //然后转向右边节点，继续上面整个过程
            } else {
                TreeNode tmp = stack.pop();
                System.out.print(tmp.val);
                root = tmp.right;
            }
        }

    }
    public void postOrderNor(TreeNode root){
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode pre = null;
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode curr = stack.peek();
            if((curr.left == null && curr.right == null) ||
                    (pre != null && (pre == curr.left || pre == curr.right))){
                //如果当前结点左右子节点为空或上一个访问的结点为当前结点的子节点时，当前结点出栈
                System.out.println(curr.val);
                pre = curr;
                stack.pop();
            }else{
                if(curr.right != null) stack.push(curr.right); //先将右结点压栈
                if(curr.left != null) stack.push(curr.left);   //再将左结点入栈
            }
        }
    }

}

